<?php
	class CategoriesView
	{
        private $controller;
        
        // Set a reference to the controller
        public function setController($controller)
        {
            $this->controller = $controller;
        }
        
        // Show top
        public function showTop()
        {
            echo '
                <h1>Categories</h1>
				<p>Manage your categories here.</p>
				<form method="post" action="index.php?categories">
            ';
        }
        
        // Show bottom
        public function showBottom()
        {
            echo '
                </form>
            ';
        }
        
        // Show add / edit form
        public function showForm($values)
        {
            echo '
                <div>
                    <label>Name: </label>	
                    <input type="text" name="name" class="text" value="'.$values['name'].'" />
                </div>
                <div>
                    <label>Description:</label>
                    <input type="text" name="description" class="text" value="'.$values['description'].'" />
                </div>
                <div>
                    <label>Datatype:</label>
                    <select name="id_datatype">
                ';				
            $types = $this->controller->getDataTypes();			
            foreach($types as $type) {
                $selected = $type['id']==$values['id_datatype'] ? ' selected="selected"' : '';
                echo '<option value="'.$type['id'].'"'.$selected.'>'.$type['name'].'</option>';
            }
            echo '
                    </select>
                </div>
                <div>
                    <label>Category:</label>
                    <select name="id_category">
					<option value="0" class="toplevel">This is a top level category</option>
                ';
            $categories = $this->controller->getCategories();
            foreach($categories as $category) {
                $selected = $category['id']==$values['value'] ? ' selected="selected"' : '';
                echo '<option value="'.$category['id'].'">'.$category['name'].'</option>';
            }
            echo '
                    </select>
                </div>
                <label>&nbsp;</label>
                <input type="hidden" name="action" value="'.$values['action'].'" />
                <input type="hidden" name="id" value="'.$values['id'].'" />
                <input type="submit" value="Save" name="save" />
            ';
        }
        
        // Show no records available:
        public function showNoRecordsAvailable() {
            echo '
                <p><em>There are no options available...</em></p>
            ';
        }
        
        // Show the records:
        public function showRecords($records) {
            echo '
                <table class="zebra optionlist">
                    <tr>
                        <th>&nbsp;</th>
                        <th>ID</th>
                        <th>Name</th>
                        <th>Description</th>
                        <th>Datatype</th>
                    </tr>
            ';
            foreach($records as $record) {
                echo '
                    <tr>
                        <td><input type="radio" name="id" value="'.$record['id'].'" /></td>
                        <td>'.$record['id'].'</td>
                        <td>'.$record['name'].'</td>
                        <td>'.$record['description'].'</td>
                        <td>'.$record['datatype'].'</td>
                    </tr>
                ';
            }
            echo '
                </table>
            ';
        }
        
        // Show Add/Edit/Delete buttons:
        public function showAddEditDelete() {
            echo '
                <input type="submit" name="edit" value="edit..." />
                <input type="submit" name="delete" value="delete" />
                <input type="submit" name="add" value="add new..." />            
            ';
        }
	}
?>